﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhSystemBLL.basicData;
using dhDataAcess.Model;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using baseSet.datGridSet;
using baseSet.importExport;
using dhsystem.Main;
using baseSet.power;

namespace dhsystem.basicForm
{
    public partial class FrmWorkerList : Form
    {
        workerListBLL workerList = new workerListBLL();
        workerListModel workerRow = new workerListModel();
        deptBLL dept = new deptBLL();

        public FrmWorkerList()
        {
            InitializeComponent();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
           //这里是为了最大化或者恢复原状的问题  朱飞  --2017--01-17
            bindingSourcedept.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedept;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
            checkAdd.Visible = true;
            if (lblState.Text == "编辑")
            {
                MessageBox.Show("当前在编辑数据状态，请先保存编辑数据或者取消再进行编辑");
                return;
            }
            lblState.Text = "新增";
            if (groupAdd.Visible == false)
            {
                groupAdd.Visible = true;
                groupList.Width = groupList.Width - 288;
                groupAdd.Location = new Point(groupList.Width+20 , groupList.Top);
            }
            textWorkernum.Focus();
           
           
        }
        //打开窗口时显示列表数据并且隐藏新增数据组件
        private void FrmWorkerList_Load(object sender, EventArgs e)
        {
            bindingSourceWorker.DataSource = workerList.getworkerListAll();
            dGridWorkerList.DataSource = bindingSourceWorker;
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            DcolumnSet.colSet(moduleInfo.workerList , dGridWorkerList );
            toolStripStatusLabel1.Text = "数据总条数:" + dGridWorkerList.RowCount.ToString() + "条";

            //textCardNum.DataBindings.Add("Text", bindingSourceWorker, "cardNum");
            //textWorkernum.DataBindings.Add("Text", bindingSourceWorker, "workerNum");

            //dGridWorkerList.Columns["recordID"].Visible = false;
            //dGridWorkerList.Columns["name"].HeaderText= "姓名";
            DcolumnSet.colSet("workerList", dGridWorkerList);

            sysDataSet.modulAaccessDataTable modulAaccessI = powerSet.modulPower(moduleInfo.workerList );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccessI)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (lblState.Text == "新增")
            {
                
                
                    if (textWorkernum.Text =="")
                    {
                        MessageBox.Show("职员工号不能为空");
                    }
                    else if (comboBoxdeptNum.Text == "")
                    {
                        MessageBox.Show("部门编号不能为空");

                    }
                    else
                    {
                        try 
                        {
                        DialogResult result = MessageBox.Show("是否新增新的员工资料！", "提示信息", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {

                            

                            workerRow.workerNum = textWorkernum.Text;
                            workerRow.name = textWorkerName.Text;
                            workerRow.sex = comboBoxsex.Text;
                            workerRow.cardnum = textCardNum.Text;
                            workerRow.birthday = dateTimeBirthday.Value;
                            workerRow.title = texttitle.Text;
                            workerRow.detpNum = comboBoxdeptNum.SelectedValue.ToString();
                            workerRow.createDate = DateTime.Now;
                            workerRow.createUser = textcreateuser.Text;
                            workerRow.updateDate = DateTime.Now;
                            workerRow.updateUser = textupdateuser.Text;
                            workerRow.mark = textmark.Text;
                            workerList.workerListInsert(workerRow);
                            bindingSourceWorker.DataSource = workerList.getworkerListAll();
                            if (checkAdd.Checked == true)
                            {
                                lblState.Text = "新增";
                            }
                            else
                            {
                                lblState.Text = "正常";
                            }
                            return;
                        }
                    }
                        catch (FormatException ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        } 
                }   
            }
            if (lblState.Text == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否更改员工资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        workerRow.workerNum = textWorkernum.Text;
                        workerRow.name = textWorkerName.Text;
                        workerRow.sex = comboBoxsex.Text;
                        workerRow.cardnum = textCardNum.Text;
                        workerRow.birthday = dateTimeBirthday.Value;
                        workerRow.title = texttitle.Text;
                        workerRow.detpNum = comboBoxdeptNum.SelectedValue.ToString();
                        workerRow.createDate = DateTime.Now;
                        workerRow.createUser = textcreateuser.Text;
                        workerRow.updateDate = DateTime.Now;
                        workerRow.updateUser = textupdateuser.Text;
                        workerRow.mark = textmark.Text;
                        workerList.workerListUpdate (workerRow);
                        bindingSourceWorker.DataSource = workerList.getworkerListAll();
                        lblState.Text = "正常";
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
               // MessageBox.Show("编辑数据保存");
               // lblState.Text = "正常";
               // return;
            }


      /*    try
            {
                DialogResult result = MessageBox.Show("是否新增新的员工资料！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    workerListModel workerRow = new workerListModel();
                    workerRow.workerNum = textWorkernum.Text;
                    workerRow.name = textWorkerName.Text;
                    workerRow.sex = comboBoxsex.Text;
                    workerRow.cardnum = textCardNum.Text;
                    workerRow.birthday = dateTimeBirthday.Value;
                    workerRow.title = texttitle.Text;
                    workerRow.detpNum = comboBoxdeptNum.SelectedValue.ToString() ;
                    workerRow.createDate = DateTime.Now;
                    workerRow.createUser = textcreateuser.Text;
                    workerRow.updateDate = DateTime.Now;
                    workerRow.updateUser = textupdateuser.Text;
                    workerRow.mark = textmark.Text;
                    workerList.workerListInsert(workerRow);
                    bindingSourceWorker.DataSource = workerList.getworkerListAll();
                }

            }
            catch (FormatException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }*/
        }
        //部门编号控件单机事件
        private void comboBoxdeptNum_Click(object sender, EventArgs e)
        {
            bindingSourcedept.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedept;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";

        }
        //取消按钮，隐藏新增数据组件
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            lblState.Text = "正常";
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string workernum = dGridWorkerList.Rows[dGridWorkerList.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除工号为"+workernum +"的员工信息！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceWorker.DataSource = workerList.workerListDel(workernum);
                    dGridWorkerList.DataSource = bindingSourceWorker;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            bindingSourceWorker.DataSource = workerList.getworkerListAll();
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSourcedept .DataSource =workerList.getworkerListAll("%" + textNameQuery.Text + "%");
                dGridWorkerList.DataSource = bindingSourcedept;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.workerList );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridWorkerList.DataSource = query.rTable;
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            bindingSourcedept.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedept;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
            comboBoxdeptNum.SelectedValue = dGridWorkerList.Rows[dGridWorkerList.CurrentRow.Index].Cells[7].Value.ToString();
            checkAdd.Visible = false;
            try
            {
                if (lblState.Text == "新增")
                {
                    MessageBox.Show("当前在新增数据状态，请先保存新增数据或者取消再进行编辑");
                    return;
                }

                lblState.Text = "编辑";

                if (groupAdd.Visible == false)
                {
                    groupAdd.Visible = true;
                    groupList.Width = groupList.Width - 288;
                    groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                }

                DataGridViewRow currentRow = dGridWorkerList.CurrentRow;
                textCardNum.Text = currentRow.Cells["cardNum"].Value.ToString();
                textWorkernum.Text = currentRow.Cells["workerNum"].Value.ToString();
                textWorkerName.Text = currentRow.Cells["name"].Value.ToString();
                comboBoxsex.Text = currentRow.Cells["sex"].Value.ToString();
                comboBoxdeptNum.Text = currentRow.Cells["detpNum"].Value.ToString();
                dateTimeBirthday.Text = currentRow.Cells["birthday"].Value.ToString();
                texttitle.Text = currentRow.Cells["title"].Value.ToString();
                textcreateuser.Text = currentRow.Cells["createUser"].Value.ToString();
                textupdateuser.Text = currentRow.Cells["updateUser"].Value.ToString();
                textmark.Text = currentRow.Cells["mark"].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void toolSet_Click(object sender, EventArgs e)
        {
            sysDataSet.workerListDataTable workerTable = (sysDataSet.workerListDataTable)bindingSourceWorker.DataSource;
            foreach (sysDataSet.workerListRow row in workerTable)
            {
                if (row.RowState == DataRowState.Added)
                {
                    MessageBox.Show("新增数据"+row.workerNum.ToString());

                }
                if (row.RowState == DataRowState.Modified)
                {
                    MessageBox.Show("修改数据"+row.workerNum.ToString());
                }
                if (row.RowState == DataRowState.Unchanged)
                {
                    MessageBox.Show("无更改");
                }
            }
        }

        private void dGridWorkerList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            gridViesSet.setGridViewRowNum(e, dGridWorkerList);
        }
        //窗口大小发生改变时发生
        private void FrmWorkerList_Resize(object sender, EventArgs e)
        {
            if (groupAdd.Visible == true)
            {
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
        }

        private void toolStripSeparator1_Click(object sender, EventArgs e)
        {

        }

        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //datagridview第一列显示行号
        private void dGridWorkerList_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmImport import = new FrmImport(moduleInfo.workerList );
            import.ShowDialog();
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.workerList , dGridWorkerList , "职员信息");
            frm.ShowDialog();
        }

     
    }
}
